package com.yanfan.maintain.controller;


import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.energy.domain.ImageUrl;
import com.yanfan.energy.domain.InventoryLog;
import com.yanfan.energy.domain.SparePartOperateVo;
import com.yanfan.energy.domain.SpareParts;
import com.yanfan.maintain.domain.SparePartsDevice;
import com.yanfan.maintain.service.SparePartsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 备品备件表(SpareParts)表控制层
 *
 * @author makejava
 * @since 2023-03-14 12:12:24
 */
@RestController
@RequestMapping("/spareParts")
public class SparePartsController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SparePartsService sparePartsService;

    /**
     * 分页查询
     *
     * @param spareParts 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    public AjaxResult queryByPage(@RequestBody SpareParts spareParts) {
        if (spareParts.getPageNum() != null || spareParts.getPageSize() != null) {
            PageHelper.startPage(spareParts.getPageNum(), spareParts.getPageSize());
        }
        PageInfo<SpareParts> pageInfo = new PageInfo<>(this.sparePartsService.queryByPage(spareParts));
        return AjaxResult.success(pageInfo);
    }


    /**
     * 分页查询
     *
     * @param
     * @return 查询结果
     */
    @PostMapping("/queryByPageList")
    public AjaxResult queryByPageList() {
        return AjaxResult.success(this.sparePartsService.queryByPage(new SpareParts()));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/queryById/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.sparePartsService.queryById(id));
    }

    /**
     * 通过主键查询出入库日志  （和易损件共用)
     *
     * @param inventoryLog 相关数据
     * @return 单条数据
     */
    @GetMapping("/queryItemLog")
    public TableDataInfo queryItemLog(InventoryLog inventoryLog) {
        startPage();
        List<InventoryLog> list = this.sparePartsService.queryItemLog(inventoryLog);
        return getDataTable(list);
    }


    /**
     * 新增数据
     *
     * @param spareParts 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SpareParts spareParts) {
        return AjaxResult.success(this.sparePartsService.insert(spareParts));
    }

    /**
     * 编辑数据
     *
     * @param spareParts 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SpareParts spareParts) {
        return AjaxResult.success(this.sparePartsService.update(spareParts));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/deleteById/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.sparePartsService.deleteById(id));
    }


    /**
     * 查询所有的设备信息
     *
     * @return
     */
    @PostMapping("/deviceList")
    public AjaxResult deviceList() {
        return AjaxResult.success(this.sparePartsService.deviceList());
    }

    /**
     * 查询所有的【所属系统】
     *
     * @return
     */
    @PostMapping("/owningSystem")
    public AjaxResult owningSystem() {
        return AjaxResult.success(this.sparePartsService.owningSystem());
    }

    /**
     * 出入库 返回出库单号
     *
     * @return
     */
    @PostMapping("/inOut")
    public AjaxResult inOut(@RequestBody InventoryLog inventoryLog) {
        return AjaxResult.success(this.sparePartsService.inOut(inventoryLog));
    }

    /**
     * 归还备件，判断是否已借用以及数量
     *
     * @return
     */
    @PostMapping("/giveBack")
    public AjaxResult giveBack(@RequestBody InventoryLog inventoryLog) {
        return toAjax(this.sparePartsService.giveBack(inventoryLog));
    }


    /**
     * 返回出入库 的出库单号    （和容易损失件共用）
     *
     * @return
     */
    @PostMapping("/inOutOrderNumber")
    public AjaxResult inOutOrderNumber(@RequestBody SparePartOperateVo vo) {
        return AjaxResult.success(this.sparePartsService.inOutOrderNumber(vo));
    }


    /**
     * 关联设备信息查询
     *
     * @return
     */
    @GetMapping("/asso-device-list")
    public TableDataInfo assoDeviceList(SparePartsDevice partsDevice) {
        startPage();
        List<SparePartsDevice> list = this.sparePartsService.selectAssoDeviceList(partsDevice);
        return getDataTable(list);
    }

    /**
     * 关联设备信息查询
     *
     * @return
     */
    @PostMapping("/asso-device")
    public AjaxResult assoDevice(@RequestBody List<SparePartsDevice> partsDeviceList) {
        int i = this.sparePartsService.batchInsertAssoDevice(partsDeviceList);
        return toAjax(i);
    }

    /**
     * 关联设备信息查询
     *
     * @return
     */
    @PostMapping("/cancel-asso-device/{assoId}")
    public AjaxResult cancelAssoDevice(@PathVariable("assoId") Integer assoId) {
        int i = this.sparePartsService.cancelAssoDevice(assoId);
        return toAjax(i);
    }


    /**
     * 关联备件图片信息查询
     *
     * @return
     */
    @GetMapping("/imgs")
    public TableDataInfo imgs(SpareParts parts) {
        List<ImageUrl> list = this.sparePartsService.selectPartImgs(parts);
        return getDataTable(list);
    }

    /**
     * 关联设备信息查询
     *
     * @return
     */
    @PostMapping("/imgs")
    public AjaxResult saveImgs(@RequestBody SpareParts parts) {
        int i = this.sparePartsService.updatePartImgs(parts);
        return toAjax(i);
    }


    /**
     * 通过主键查询出入库日志  （和易损件共用)
     *
     * @param inventoryLog 相关数据
     * @return 单条数据
     */
    @GetMapping("/queryPartsLog")
    public TableDataInfo queryPartsLog(InventoryLog inventoryLog) {
        startPage();
        List<InventoryLog> list = this.sparePartsService.queryPartsLog(inventoryLog);
        return getDataTable(list);
    }
}

